package slim3.demo.controller.six;


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;



public class ReadXMLFile {
	
//    public static void main(String[] args) throws Exception {
//      //get all
//        ReadXMLFile test = new ReadXMLFile();
//        ArrayList<FlightBean_Intl> beans = test.getDeparture();
//        
//        for (int i=0; i<beans.size(); i++) {
//            System.out.println(beans.get(i).toString());
//        }
//    }
    
    
    /**
     * get international departure
     * @return
     * @throws Exception
     */
    public ArrayList<FlightBean_Intl> getDeparture() throws Exception{

        ArrayList<FlightBean_Intl> all = new ArrayList<FlightBean_Intl>();
        FlightBean_Intl bean = null;
        

        try {

          
            /**
             * read from URL
             */
            URL tsa = new URL("http://www.tsa.gov.tw/CustomerSet/tsa/tp_FlightScheduleInternational/u_fsout_v.asp?id={3DB46F7C-D183-4BF7-9535-155C1678F4A1}");
                        
            URLConnection urlc = tsa.openConnection();
            urlc.setRequestProperty("Content-Type", "text/xml;charset=UTF-8");
            urlc.setDoOutput(true);
            urlc.setDoInput(true);
            
//          BufferedReader in = new BufferedReader(
//              new InputStreamReader(tsa.openStream()));
            BufferedReader in = new BufferedReader(new InputStreamReader(urlc.getInputStream()));

//          String inputLine;
//          while ((inputLine = in.readLine()) != null)
//              System.out.println(inputLine);
//          in.close();
  
    
            /**
             * read from file
             */
            //BufferedReader in = new BufferedReader(new FileReader("RSS.xml"));
  
  
            /**
             * process code below
             */
            String str;
            boolean finished = false;

                while ( ((str = in.readLine()) != null) && !finished) {
                    
                    if (str.trim().equals("<a href=\"/CustomerSet/tsa/tp_FlightScheduleInternational/RSS_news.asp?id={3DB46F7C-D183-4BF7-9535-155C1678F4A1}&ifs=1\">")) {
                                        
                     while ((str = in.readLine()) != null) {
                         
                         if (str.equals("</table>")) {
                             finished = true;
                             break;
                         }
                         
                         
                         if (str.contains("<td align=\"center\" class=\"C-tableA4")) {
                             String a, b, c, d, e, f;
                             a = str;
                             b = in.readLine();
                             c = in.readLine();
                             d = in.readLine();
                             e = in.readLine();
                             f = in.readLine();
                             
                             bean = addFlightBeanDeparture(a,b,c,d,e,f);
                             
                             all.add(bean);
                             
                             
                         }
                
                         
                     }
                     
                }
    
            //System.out.println(str);
            }
            
            in.close();
            
            //System.out.println(html);
            
            
            
            } catch (IOException e) {
                e.getStackTrace();
            }
      
        return all;
            
    }
    
    
    public FlightBean_Intl addFlightBeanDeparture(String a, String b, String c, String d, String e, String f) {
        
        String t;
        FlightBean_Intl bean = new FlightBean_Intl();
        
        //airline
        //<td align="center" class="C-tableA4">復興<br>GE   </td>
        t = a;
//      System.out.println(t);
        t = t.substring(t.indexOf("br>")+3, t.indexOf("</")).trim();
//      t = t.substring(0, 2) + t.substring(6, 8);
//      System.out.println(t);
        bean.setAirline(t);
        
        //destination
        //<td align="center" class="C-tableA4">浦東<br>PVG</td>
        t = b;
//      System.out.println(t);
//      t = t.substring(t.indexOf(">")+1, t.indexOf("</")).trim();
        t = t.substring(t.indexOf("br>")+3, t.indexOf("</t")).trim();
//      t = t.substring(0, 2) + t.substring(6, 9);
//      System.out.println(t);
        bean.setDestination(t);
        bean.setOrigin("");
        
        //flight no
        //<td align="center" class="C-tableA4">332</td>
        t = c;
//      System.out.println(t);
//      System.out.println(t.substring(t.indexOf(">")+1, t.indexOf("</")));
        bean.setFlightNo(t.substring(t.indexOf(">")+1, t.indexOf("</")));
                
        //sched time
        //<td align="center" class="C-tableA4">0715</td>
        t = d;
//      System.out.println(t);
//      System.out.println(t.substring(t.indexOf(">")+1, t.indexOf("</")));
        bean.setSchedTime(t.substring(t.indexOf(">")+1, t.indexOf("</")));
        
        //actual time
        //<td align="center" class="C-tableA4">0715</td>
        t = e;
//      System.out.println(t);
//      System.out.println(t.substring(t.indexOf(">")+1, t.indexOf("</")));
        bean.setActualTime(t.substring(t.indexOf(">")+1, t.indexOf("</")));
        
        //status
        //<td align="center" class="C-tableA4">已飛Departed</td>
        t = f;
//      System.out.println(t);
//      System.out.println(t.substring(t.indexOf(">")+1, t.indexOf("</")));
//      bean.setStatus(t.substring(t.indexOf(">")+3, t.indexOf("</")));
        String t2 = t.substring(t.indexOf(">")+1, t.indexOf("</"));
        if (t2.equals("轉降桃園")) {
            bean.setStatus("LandInTaoYuan");
        } else if (t2.equals("航班測試TEST")) {
            bean.setStatus("TEST");
        } else if (t2.equals("轉降高雄")) {
            bean.setStatus("LandInGaoXiong");
        } else if (t2.equals("轉降北竿")) {
            bean.setStatus("LandInBeiGan");
        } else if (t2.equals("由桃園機場搭機")) {
            bean.setStatus("FlyFromTaoYuan");
        } else if (t2.equals("轉降臺中")) {
            bean.setStatus("LandInTaiZhong");
        } else if (t2.equals("時間更改")) {
            bean.setStatus("ScheduleChanged");
        } else {
            bean.setStatus(t.substring(t.indexOf(">")+3, t.indexOf("</")));          
        }
        
//      System.out.println(bean);
        

        return bean;
        
  }
    
    /**
     * get international arrival
     * @return
     * @throws Exception
     */
    public ArrayList<FlightBean_Intl> getArrival() throws Exception{

        ArrayList<FlightBean_Intl> all = new ArrayList<FlightBean_Intl>();
        FlightBean_Intl bean = null;
        

        try {

          
            /**
             * read from URL
             */
            URL tsa = new URL("http://www.tsa.gov.tw/CustomerSet/tsa/tp_FlightScheduleInternational/u_fsin_v.asp?id={3DB46F7C-D183-4BF7-9535-155C1678F4A1}");
                        
            URLConnection urlc = tsa.openConnection();
            urlc.setRequestProperty("Content-Type", "text/xml;charset=UTF-8");
            urlc.setDoOutput(true);
            urlc.setDoInput(true);
            
//          BufferedReader in = new BufferedReader(
//              new InputStreamReader(tsa.openStream()));
            BufferedReader in = new BufferedReader(new InputStreamReader(urlc.getInputStream()));

//          String inputLine;
//          while ((inputLine = in.readLine()) != null)
//              System.out.println(inputLine);
//          in.close();
  
    
            /**
             * read from file
             */
            //BufferedReader in = new BufferedReader(new FileReader("RSS.xml"));
  
  
            /**
             * process code below
             */
            String str;
            boolean finished = false;

                while ( ((str = in.readLine()) != null) && !finished) {
                    
                    if (str.trim().equals("<a href=\"/CustomerSet/tsa/tp_FlightScheduleInternational/RSS_news.asp?id={3DB46F7C-D183-4BF7-9535-155C1678F4A1}&ifs=2\">")) {
                    
                     while ((str = in.readLine()) != null) {
                         
                         if (str.equals("</table>")) {
                             finished = true;
                             break;
                         }
                         
                         
                         if (str.contains("<td align=\"center\" class=\"C-tableA4")) {
                             String a, b, c, d, e, f, g;
                             a = str;
                             b = in.readLine();
                             c = in.readLine();
                             d = in.readLine();
                             e = in.readLine();
                             f = in.readLine();
                             g = in.readLine();
                             
                             bean = addFlightBeanArrival(a,b,c,d,e,f,g);
                             
                             all.add(bean);


                         }
                
                         
                     }
                     
                }
    
            }
            
            in.close();
            
            
            } catch (IOException e) {
                e.getStackTrace();
            }
      
        return all;
            
    }
      
      
    public FlightBean_Intl addFlightBeanArrival(String a, String b, String c, String d, String e, String f, String g) {
          
          String t;
          FlightBean_Intl bean = new FlightBean_Intl();
          
          //airline
          //<td align="center" class="C-tableA4">日航<br>JL   </td>
          t = a;
//        System.out.println(t);
          t = t.substring(t.indexOf("br>")+3, t.indexOf("</")).trim();
//        t = t.substring(0, 2) + t.substring(6, 8);
//        System.out.println(t);
          bean.setAirline(t);
          
          //origin
          //<td align="center" class="C-tableA4">羽田<br>HND</td>
          t = b;
//        System.out.println(t);
//        t = t.substring(t.indexOf(">")+1, t.indexOf("</")).trim();
          t = t.substring(t.indexOf("br>")+3, t.indexOf("</t")).trim();
//        t = t.substring(0, 2) + t.substring(6, 9);
//        System.out.println(t);
          bean.setOrigin(t);
          bean.setDestination("");
          
          //flight no
          //<td align="center" class="C-tableA4">5041</td>
          t = c;
//        System.out.println(t);
//        System.out.println(t.substring(t.indexOf(">")+1, t.indexOf("</")));
          bean.setFlightNo(t.substring(t.indexOf(">")+1, t.indexOf("</")));
          
          //departure time - ignored
          //<td align="center" class="C-tableA4">--</td>
          t = d;
          
          //sched time
          //<td align="center" class="C-tableA4">1000</td>
          t = e;
//        System.out.println(t);
//        System.out.println(t.substring(t.indexOf(">")+1, t.indexOf("</")));
          bean.setSchedTime(t.substring(t.indexOf(">")+1, t.indexOf("</")));
          
          //actual time
          //<td align="center" class="C-tableA4">0920</td>
          t = f;
//        System.out.println(t);
//        System.out.println(t.substring(t.indexOf(">")+1, t.indexOf("</")));
          bean.setActualTime(t.substring(t.indexOf(">")+1, t.indexOf("</")));
          
          //status
          //<td align="center" class="C-tableA4">已到Arrived</td>
          t = g;
//        System.out.println(t);
//        System.out.println(t.substring(t.indexOf(">")+1, t.indexOf("</")));
//        bean.setStatus(t.substring(t.indexOf(">")+3, t.indexOf("</")));
          String t2 = t.substring(t.indexOf(">")+1, t.indexOf("</"));
          if (t2.equals("轉降桃園")) {
              bean.setStatus("LandInTaoYuan");
          } else if (t2.equals("航班測試TEST")) {
              bean.setStatus("TEST");
          } else if (t2.equals("轉降高雄")) {
              bean.setStatus("LandInGaoXiong");
          } else if (t2.equals("轉降北竿")) {
              bean.setStatus("LandInBeiGan");
          } else if (t2.equals("由桃園機場搭機")) {
              bean.setStatus("FlyFromTaoYuan");
          } else if (t2.equals("轉降臺中")) {
              bean.setStatus("LandInTaiZhong");
          } else if (t2.equals("時間更改")) {
              bean.setStatus("ScheduleChanged");
          } else {
              bean.setStatus(t.substring(t.indexOf(">")+3, t.indexOf("</")));
          }
          
//        System.out.println(bean);
          

          return bean;
          
    }
    
	
	/**
	 * get domestic departure - only knh
	 * @return
	 * @throws Exception
	 */
    public ArrayList<FlightBean> getKNH() throws Exception{

	    ArrayList<FlightBean> all = new ArrayList<FlightBean>();
	    FlightBean bean = null;
	    String html = "";
	    

		try {

		  
			/**
			 * read from URL
			 */
			URL tsa = new URL("http://www.tsa.gov.tw/CustomerSet/tsa/tp_FlightSchedule/RSS_news.asp?id={FE8E2C05-9D2C-482F-9919-4A3A3387E71A}&dfc=1");
						
			URLConnection urlc = tsa.openConnection();
			urlc.setRequestProperty("Content-Type", "text/xml;charset=UTF-8");
			urlc.setDoOutput(true);
			urlc.setDoInput(true);
			
//			BufferedReader in = new BufferedReader(
//	            new InputStreamReader(tsa.openStream()));
			BufferedReader in = new BufferedReader(new InputStreamReader(urlc.getInputStream()));

//	        String inputLine;
//	        while ((inputLine = in.readLine()) != null)
//	            System.out.println(inputLine);
//	        in.close();
  
    
			/**
			 * read from file
			 */
			//BufferedReader in = new BufferedReader(new FileReader("RSS.xml"));
  
  
			/**
			 * process code below
			 */
			String str;
			boolean finished = false;

				while ( ((str = in.readLine()) != null) && !finished) {
				    
					if (str.equals("<![CDATA[<table width=100% border=1 cellpadding=2 cellspacing=1><tr>")) {
					html = html + "<table width=100% border=1 cellpadding=2 cellspacing=1><tr>\n";
					
					 while ((str = in.readLine()) != null) {
						 
						 if (str.equals("</table>]]>")) {
							 html = html + "</table>";
							 finished = true;
							 break;
						 }
						 
						 
						 if (str.contains("<td align")) {
							 String a, b, c, d, e, f;
							 a = str;
							 b = in.readLine();
							 c = in.readLine();
							 d = in.readLine();
							 e = in.readLine();
							 f = in.readLine();
							 
							 bean = addFlightBean(a,b,c,d,e,f);
							 
							 if (bean.getDestination().equals("KNH")) {
							     
	                             all.add(bean);
	                             
							 }
							 
							 html = html + a + "\n" + b + "\n" + c + "\n" + d + "\n" + e + "\n" + f + "\n";
							 
						 } else {
				
				    		 html = html + str + "\n";
						 }
				
						 
					 }
					 
				}
	
			//System.out.println(str);
			}
			
			in.close();
			
			//System.out.println(html);
		    
		    
		    
			} catch (IOException e) {
			    e.getStackTrace();
			}
	  
		return all;
			
	}
	  
	  /**
	   * get domestic departure - only mfk and lzn
	   * @return
	   * @throws Exception
	   */
	public ArrayList<FlightBean> getMFKandLZN() throws Exception{

        ArrayList<FlightBean> all = new ArrayList<FlightBean>();
        FlightBean bean = null;
        String html = "";
        

        try {

          
            /**
             * read from URL
             */
            URL tsa = new URL("http://www.tsa.gov.tw/CustomerSet/tsa/tp_FlightSchedule/RSS_news.asp?id={FE8E2C05-9D2C-482F-9919-4A3A3387E71A}&dfc=1");
                        
            URLConnection urlc = tsa.openConnection();
            urlc.setRequestProperty("Content-Type", "text/xml;charset=UTF-8");
            urlc.setDoOutput(true);
            urlc.setDoInput(true);
            
//          BufferedReader in = new BufferedReader(
//              new InputStreamReader(tsa.openStream()));
            BufferedReader in = new BufferedReader(new InputStreamReader(urlc.getInputStream()));

//          String inputLine;
//          while ((inputLine = in.readLine()) != null)
//              System.out.println(inputLine);
//          in.close();
  
    
            /**
             * read from file
             */
            //BufferedReader in = new BufferedReader(new FileReader("RSS.xml"));
  
  
            /**
             * process code below
             */
            String str;
            boolean finished = false;

                while ( ((str = in.readLine()) != null) && !finished) {
                    
                    if (str.equals("<![CDATA[<table width=100% border=1 cellpadding=2 cellspacing=1><tr>")) {
                    html = html + "<table width=100% border=1 cellpadding=2 cellspacing=1><tr>\n";
                    
                     while ((str = in.readLine()) != null) {
                         
                         if (str.equals("</table>]]>")) {
                             html = html + "</table>";
                             finished = true;
                             break;
                         }
                         
                         
                         if (str.contains("<td align")) {
                             String a, b, c, d, e, f;
                             a = str;
                             b = in.readLine();
                             c = in.readLine();
                             d = in.readLine();
                             e = in.readLine();
                             f = in.readLine();
                             
                             bean = addFlightBean(a,b,c,d,e,f);
                             
                             if (bean.getDestination().equals("MFK") ||
                                 bean.getDestination().equals("LZN")    ) {
                                 
                                 all.add(bean);
                                 
                             }
                             
                             html = html + a + "\n" + b + "\n" + c + "\n" + d + "\n" + e + "\n" + f + "\n";
                             
                         } else {
                
                             html = html + str + "\n";
                         }
                
                         
                     }
                     
                }
    
            //System.out.println(str);
            }
            
            in.close();
            
            //System.out.println(html);
            
            
            
            } catch (IOException e) {
                e.getStackTrace();
            }
      
        return all;
            
    }
	
	/**
	 * get domestic departure - only knh, mfk, and lzn
	 * @return
	 * @throws Exception
	 */
	public ArrayList<FlightBean> getKNHandMFKandLZN() throws Exception{

        ArrayList<FlightBean> all = new ArrayList<FlightBean>();
        FlightBean bean = null;
        String html = "";
        

        try {

          
            /**
             * read from URL
             */
            URL tsa = new URL("http://www.tsa.gov.tw/CustomerSet/tsa/tp_FlightSchedule/RSS_news.asp?id={FE8E2C05-9D2C-482F-9919-4A3A3387E71A}&dfc=1");
                        
            URLConnection urlc = tsa.openConnection();
            urlc.setRequestProperty("Content-Type", "text/xml;charset=UTF-8");
            urlc.setDoOutput(true);
            urlc.setDoInput(true);
            
//          BufferedReader in = new BufferedReader(
//              new InputStreamReader(tsa.openStream()));
            BufferedReader in = new BufferedReader(new InputStreamReader(urlc.getInputStream()));

//          String inputLine;
//          while ((inputLine = in.readLine()) != null)
//              System.out.println(inputLine);
//          in.close();
  
    
            /**
             * read from file
             */
            //BufferedReader in = new BufferedReader(new FileReader("RSS.xml"));
  
  
            /**
             * process code below
             */
            String str;
            boolean finished = false;

                while ( ((str = in.readLine()) != null) && !finished) {
                    
                    if (str.equals("<![CDATA[<table width=100% border=1 cellpadding=2 cellspacing=1><tr>")) {
                    html = html + "<table width=100% border=1 cellpadding=2 cellspacing=1><tr>\n";
                    
                     while ((str = in.readLine()) != null) {
                         
                         if (str.equals("</table>]]>")) {
                             html = html + "</table>";
                             finished = true;
                             break;
                         }
                         
                         
                         if (str.contains("<td align")) {
                             String a, b, c, d, e, f;
                             a = str;
                             b = in.readLine();
                             c = in.readLine();
                             d = in.readLine();
                             e = in.readLine();
                             f = in.readLine();
                             
                             bean = addFlightBean(a,b,c,d,e,f);
                             
                             if (bean.getDestination().equals("KNH") ||
                                 bean.getDestination().equals("MFK") ||
                                 bean.getDestination().equals("LZN")    ) {
                                 
                                 all.add(bean);
                                 
                             }
                             
                             html = html + a + "\n" + b + "\n" + c + "\n" + d + "\n" + e + "\n" + f + "\n";
                             
                         } else {
                
                             html = html + str + "\n";
                         }
                
                         
                     }
                     
                }
    
            //System.out.println(str);
            }
            
            in.close();
            
            //System.out.println(html);
            
            
            
            } catch (IOException e) {
                e.getStackTrace();
            }
      
        return all;
            
    }
	
	
	/**
	 * get all domestic departure
	 * @return
	 * @throws Exception
	 */
	public ArrayList<FlightBean> getAll() throws Exception{

        ArrayList<FlightBean> all = new ArrayList<FlightBean>();
        FlightBean bean = null;
        String html = "";
        

        try {

          
            /**
             * read from URL
             */
            URL tsa = new URL("http://www.tsa.gov.tw/CustomerSet/tsa/tp_FlightSchedule/RSS_news.asp?id={FE8E2C05-9D2C-482F-9919-4A3A3387E71A}&dfc=1");
                        
            URLConnection urlc = tsa.openConnection();
            urlc.setRequestProperty("Content-Type", "text/xml;charset=UTF-8");
            urlc.setDoOutput(true);
            urlc.setDoInput(true);
            
//          BufferedReader in = new BufferedReader(
//              new InputStreamReader(tsa.openStream()));
            BufferedReader in = new BufferedReader(new InputStreamReader(urlc.getInputStream()));

//          String inputLine;
//          while ((inputLine = in.readLine()) != null)
//              System.out.println(inputLine);
//          in.close();
  
    
            /**
             * read from file
             */
            //BufferedReader in = new BufferedReader(new FileReader("RSS.xml"));
  
  
            /**
             * process code below
             */
            String str;
            boolean finished = false;

                while ( ((str = in.readLine()) != null) && !finished) {
                    
                    if (str.equals("<![CDATA[<table width=100% border=1 cellpadding=2 cellspacing=1><tr>")) {
                    html = html + "<table width=100% border=1 cellpadding=2 cellspacing=1><tr>\n";
                    
                     while ((str = in.readLine()) != null) {
                         
                         if (str.equals("</table>]]>")) {
                             html = html + "</table>";
                             finished = true;
                             break;
                         }
                         
                         
                         if (str.contains("<td align")) {
                             String a, b, c, d, e, f;
                             a = str;
                             b = in.readLine();
                             c = in.readLine();
                             d = in.readLine();
                             e = in.readLine();
                             f = in.readLine();
                             
                             bean = addFlightBean(a,b,c,d,e,f);
                             
                             all.add(bean);
                             
                             html = html + a + "\n" + b + "\n" + c + "\n" + d + "\n" + e + "\n" + f + "\n";
                             
                         } else {
                
                             html = html + str + "\n";
                         }
                
                         
                     }
                     
                }
    
            //System.out.println(str);
            }
            
            in.close();
            
            //System.out.println(html);
            
            
            
            } catch (IOException e) {
                e.getStackTrace();
            }
      
        return all;
            
    }
	  
	  
	public FlightBean addFlightBean(String a, String b, String c, String d, String e, String f) {
		  
		  String t;
		  FlightBean bean = new FlightBean();
		  
		  //airline
		  //t = "<td align=\"center\" >華信<br>AE   </td>";
		  t = a;
//		  System.out.println(t);
		  t = t.substring(t.indexOf("br>")+3, t.indexOf("</")).trim();
//		  t = t.substring(0, 2) + t.substring(6, 8);
//		  System.out.println(t);
		  bean.setAirline(t);
		  
		  //destination
		  //t = "<td align=\"center\" >馬公<br>MZG</td>";
		  t = b;
//		  System.out.println(t);
//		  t = t.substring(t.indexOf(">")+1, t.indexOf("</")).trim();
          t = t.substring(t.indexOf("br>")+3, t.indexOf("</t")).trim();
//		  t = t.substring(0, 2) + t.substring(6, 9);
//		  System.out.println(t);
		  bean.setDestination(t);
		  
		  //flight no
		  //t = "<td align=\"center\" >369</td>";
		  t = c;
//		  System.out.println(t);
//		  System.out.println(t.substring(t.indexOf(">")+1, t.indexOf("</")));
		  bean.setFlightNo(t.substring(t.indexOf(">")+1, t.indexOf("</")));
		  
		  //sched time
		  //t = "<td align=\"center\" >2010</td>";
		  t = d;
//		  System.out.println(t);
//		  System.out.println(t.substring(t.indexOf(">")+1, t.indexOf("</")));
		  bean.setSchedTime(t.substring(t.indexOf(">")+1, t.indexOf("</")));
		  
		  //actual time
		  //t = "<td align=\"center\" >2010</td>";
		  t = e;
//		  System.out.println(t);
//		  System.out.println(t.substring(t.indexOf(">")+1, t.indexOf("</")));
		  bean.setActualTime(t.substring(t.indexOf(">")+1, t.indexOf("</")));
		  
		  //status
		  //t = "<td align=\"center\" >準時On Time</td>";
		  t = f;
//		  System.out.println(t);
//		  System.out.println(t.substring(t.indexOf(">")+1, t.indexOf("</")));
//		  bean.setStatus(t.substring(t.indexOf(">")+3, t.indexOf("</")));
          String t2 = t.substring(t.indexOf(">")+1, t.indexOf("</"));
          if (t2.equals("轉降桃園")) {
              bean.setStatus("LandInTaoYuan");
          } else if (t2.equals("航班測試TEST")) {
              bean.setStatus("TEST");
          } else if (t2.equals("轉降高雄")) {
              bean.setStatus("LandInGaoXiong");
          } else if (t2.equals("轉降北竿")) {
              bean.setStatus("LandInBeiGan");
          } else if (t2.equals("由桃園機場搭機")) {
              bean.setStatus("FlyFromTaoYuan");
          } else if (t2.equals("轉降臺中")) {
              bean.setStatus("LandInTaiZhong");
          } else if (t2.equals("時間更改")) {
              bean.setStatus("ScheduleChanged");
	        } else {
	            bean.setStatus(t.substring(t.indexOf(">")+3, t.indexOf("</")));          
	        }
		  
//		  System.out.println(bean);
		  

		  return bean;
		  
	}
	
	
	/**
     * get domestic arrival
     * @return
     * @throws Exception
     */
    public ArrayList<FlightBean_DomesticArrival> getAllArrival() throws Exception{

        ArrayList<FlightBean_DomesticArrival> all = new ArrayList<FlightBean_DomesticArrival>();
        FlightBean_DomesticArrival bean = null;
        

        try {

          
            /**
             * read from URL
             */
            URL tsa = new URL("http://www.tsa.gov.tw/CustomerSet/tsa/tp_FlightSchedule/u_fsin_v.asp?id={FE8E2C05-9D2C-482F-9919-4A3A3387E71A}");
                        
            URLConnection urlc = tsa.openConnection();
            urlc.setRequestProperty("Content-Type", "text/xml;charset=UTF-8");
            urlc.setDoOutput(true);
            urlc.setDoInput(true);
            
//          BufferedReader in = new BufferedReader(
//              new InputStreamReader(tsa.openStream()));
            BufferedReader in = new BufferedReader(new InputStreamReader(urlc.getInputStream()));

//          String inputLine;
//          while ((inputLine = in.readLine()) != null)
//              System.out.println(inputLine);
//          in.close();
  
    
            /**
             * read from file
             */
            //BufferedReader in = new BufferedReader(new FileReader("RSS.xml"));
  
  
            /**
             * process code below
             */
            String str;
            boolean finished = false;

                while ( ((str = in.readLine()) != null) && !finished) {
                    
                    if (str.trim().equals("<a href=\"/CustomerSet/tsa/tp_FlightSchedule/RSS_news.asp?id={FE8E2C05-9D2C-482F-9919-4A3A3387E71A}&dfc=2\">")) {
                                        
                     while ((str = in.readLine()) != null) {
                         
                         if (str.equals("</table>")) {
                             finished = true;
                             break;
                         }
                         
                         
                         if (str.contains("<td align=\"center\" class=\"C-tableA4")) {
                             String a, b, c, d, e, f, g;
                             a = str;
                             b = in.readLine();
                             c = in.readLine();
                             d = in.readLine();
                             e = in.readLine();
                             f = in.readLine();
                             g = in.readLine();
                             
                             bean = addFlightBeanDomesticArrival(a,b,c,d,e,f,g);
                             
                             all.add(bean);
                             
                             
                         }
                
                         
                     }
                     
                }
    
            //System.out.println(str);
            }
            
            in.close();
            
            //System.out.println(html);
            
            
            
            } catch (IOException e) {
                e.getStackTrace();
            }
      
        return all;
            
    }
    
    
    public FlightBean_DomesticArrival addFlightBeanDomesticArrival(String a, String b, String c, String d, String e, String f, String g) {
        
        String t;
        FlightBean_DomesticArrival bean = new FlightBean_DomesticArrival();
        
        //airline
        //<td align="center" class="C-tableA4">華信<br>AE   </td>
        t = a;
//      System.out.println(t);
        t = t.substring(t.indexOf("br>")+3, t.indexOf("</")).trim();
//      t = t.substring(0, 2) + t.substring(6, 8);
//      System.out.println(t);
        bean.setAirline(t);
        
        //origin
        //<td align="center" class="C-tableA4">馬公<br>MZG</td>
        t = b;
//      System.out.println(t);
//      t = t.substring(t.indexOf(">")+1, t.indexOf("</")).trim();
        t = t.substring(t.indexOf("br>")+3, t.indexOf("</t")).trim();
//      t = t.substring(0, 2) + t.substring(6, 9);
//      System.out.println(t);
        bean.setOrigin(t);
        
        //flight no
        //<td align="center" class="C-tableA4">362</td>
        t = c;
//      System.out.println(t);
//      System.out.println(t.substring(t.indexOf(">")+1, t.indexOf("</")));
        bean.setFlightNo(t.substring(t.indexOf(">")+1, t.indexOf("</")));
        
        //departure time
        //<td align="center" class="C-tableA4">0730</td>
        t = d;
//      System.out.println(t);
//      System.out.println(t.substring(t.indexOf(">")+1, t.indexOf("</")));
        bean.setSchedDepartureTime(t.substring(t.indexOf(">")+1, t.indexOf("</")));
        
        //sched arrival time
        //<td align="center" class="C-tableA4">0815</td>
        t = e;
//      System.out.println(t);
//      System.out.println(t.substring(t.indexOf(">")+1, t.indexOf("</")));
        bean.setSchedArrivalTime(t.substring(t.indexOf(">")+1, t.indexOf("</")));
        
        //actual arrival time
        //<td align="center" class="C-tableA4">0810</td>
        t = f;
//      System.out.println(t);
//      System.out.println(t.substring(t.indexOf(">")+1, t.indexOf("</")));
        bean.setActualArrivalTime(t.substring(t.indexOf(">")+1, t.indexOf("</")));
        
        //status
        //<td align="center" class="C-tableA4">已到Arrived</td>
        t = g;
//      System.out.println(t);
//      System.out.println(t.substring(t.indexOf(">")+1, t.indexOf("</")));
//      bean.setStatus(t.substring(t.indexOf(">")+3, t.indexOf("</")));
        String t2 = t.substring(t.indexOf(">")+1, t.indexOf("</"));
        if (t2.equals("轉降桃園")) {
            bean.setStatus("LandInTaoYuan");
        } else if (t2.equals("航班測試TEST")) {
            bean.setStatus("TEST");
        } else if (t2.equals("轉降高雄")) {
            bean.setStatus("LandInGaoXiong");
        } else if (t2.equals("轉降北竿")) {
            bean.setStatus("LandInBeiGan");
        } else if (t2.equals("由桃園機場搭機")) {
            bean.setStatus("FlyFromTaoYuan");
        } else if (t2.equals("轉降臺中")) {
            bean.setStatus("LandInTaiZhong");
        } else if (t2.equals("時間更改")) {
            bean.setStatus("ScheduleChanged");
        } else {
            bean.setStatus(t.substring(t.indexOf(">")+3, t.indexOf("</")));          
        }
        
//      System.out.println(bean);
        

        return bean;
        
  }

}
